package com.lw.common.config;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
public class JSONFileReader {

    public JSONObject readJSONFile() {
        try {
            File file = new File("config/param.json");
            byte[] bytes;
            if (file.exists()) {
                System.out.println("config下找到");
                bytes = FileUtil.readBytes(file);
            } else {
                System.out.println("resourrce下找到");
                ClassLoader classLoader = getClass().getClassLoader();
                bytes = IoUtil.readBytes(classLoader.getResourceAsStream("param.json"));
            }
            String str = new String(bytes);
            return  JSONObject.parseObject(str);
        } catch (IORuntimeException e) {
            throw new RuntimeException(e);
        }
    }
}
